package com.eastfair.venueservice.service.impl;

import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.api.ReportDrawingCategoryAuditFeign;
import com.eastfair.home.dto.ReportDrawingCategoryAuditDTO;
import com.eastfair.home.enumeration.ReportDrawingCategoryAuditDrawingAuditStatusEnum;
import com.eastfair.home.enumeration.ReportDrawingCategoryAuditVenueAuditStatusEnum;
import com.eastfair.venueservice.entity.HomeBuilderDrawingCategoryAudit;
import com.eastfair.venueservice.service.VenueManageReportDrawingCategoryAuditService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 搭建商报馆图纸分类审核 service.
 *
 * @author dqq
 * @date 2023 /4/28
 */
@Service
@Slf4j
public class VenueManageReportDrawingCategoryAuditServiceImpl implements VenueManageReportDrawingCategoryAuditService {

    @Resource
    private ReportDrawingCategoryAuditFeign reportDrawingCategoryAuditFeign;

    @Override
    public boolean updateVenueAuditStatus(ReportDrawingCategoryAuditDTO reportDrawingCategoryAuditDTO) {
        log.info("updateVenueAuditStatus - 更新图纸分类报馆审核状态, reportDrawingCategoryAuditDTO={}", reportDrawingCategoryAuditDTO);
        R<Boolean> result = reportDrawingCategoryAuditFeign.updateVenueAuditStatus(reportDrawingCategoryAuditDTO);
        log.info("updateVenueAuditStatus - 更新图纸分类报馆审核状态, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean updateVenueAuditStatusByHomeBuilderDrawingCategory(HomeBuilderDrawingCategoryAudit audit) {
        ReportDrawingCategoryAuditDTO reportDrawingCategoryAuditDTO = new ReportDrawingCategoryAuditDTO();
        reportDrawingCategoryAuditDTO.setReportSpaceId(audit.getReportId());
        reportDrawingCategoryAuditDTO.setDrawingCategoryId(audit.getDrawingId());
        ReportDrawingCategoryAuditVenueAuditStatusEnum venueAuditStatus = ReportDrawingCategoryAuditVenueAuditStatusEnum.get(audit.getAuditStatus().getCode());
        reportDrawingCategoryAuditDTO.setVenueAuditStatus(venueAuditStatus);
        reportDrawingCategoryAuditDTO.setVenueAuditOpinion(audit.getAuditOpinion());
        reportDrawingCategoryAuditDTO.setVenueAuditUserId(audit.getAuditUserId());
        reportDrawingCategoryAuditDTO.setVenueAuditUserName(audit.getAuditUserName());
        reportDrawingCategoryAuditDTO.setVenueAuditTime(audit.getAuditTime());
        // 当前图纸分类的总体审核状态，场馆审核通过则总体通过，场馆审核未通过则总体未通过
        if (ReportDrawingCategoryAuditVenueAuditStatusEnum.APPROVED.eq(reportDrawingCategoryAuditDTO.getVenueAuditStatus())) {
            reportDrawingCategoryAuditDTO.setDrawingAuditStatus(ReportDrawingCategoryAuditDrawingAuditStatusEnum.APPROVED);
        } else {
            reportDrawingCategoryAuditDTO.setDrawingAuditStatus(ReportDrawingCategoryAuditDrawingAuditStatusEnum.NOT_APPROVED);
        }
        return updateVenueAuditStatus(reportDrawingCategoryAuditDTO);
    }
}